package com.huage.courseplatform.application.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.huage.courseplatform.application.model.dto.BusinessDTO;
import com.huage.courseplatform.application.model.dto.TeacherDTO;
import com.huage.courseplatform.application.model.entity.Business;
import com.huage.courseplatform.application.model.entity.Teacher;
import com.huage.courseplatform.application.model.param.TeacherPARAM;
import com.huage.courseplatform.application.model.vo.BusinessTeacherFlowRecordVO;
import com.huage.courseplatform.application.service.TeacherService;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.api.AbstractController;
import com.huage.courseplatform.framework.api.BaseController;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.model.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/teacher")
@Api(tags = "教师接口")
@Slf4j
public class TeacherController extends AbstractController<Teacher,TeacherDTO,TeacherPARAM> {

    @Resource
    TeacherService teacherService;

    @RepeatSubmit
    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
    @ApiOperation(value = "申请加入商户")
    @PostMapping(value = "/applyBusiness")
    public ResultUtils<String> applyBusiness(@NotEmpty(message = "教师id不能为空") String teacherId,
                                             @NotEmpty(message = "商户id不能为空") String businessId) {

        teacherService.applyBusinessBindTeacher(teacherId,businessId);
        return ResultUtils.success("申请成功");
    }


    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
    @ApiOperation(value = "根据id查询商家申请记录")
    @GetMapping(value = "/getTeacherApplyListById/{id}")
    public ResultUtils<List<BusinessTeacherFlowRecordVO>> getTeacherApplyListById(@PathVariable  String id) {
        Assert.isTrue(checkUserTeacher(id),"用户权限不足");
        List<BusinessTeacherFlowRecordVO> teacherApplyList = teacherService.getTeacherApplyList(id);

        return ResultUtils.success(teacherApplyList);
    }

//    @SaCheckRole({"ROLE_ADMIN","ROLE_TEACHER"})
//    @ApiOperation(value = "根据id查询商家列表")
//    @GetMapping(value = "/getBusinessListById/{id}")
//    public ResultUtils<List<BusinessDTO>> getTeacherApplyListById(@PathVariable  String id) {
//        Assert.isTrue(checkUserTeacher(id),"用户权限不足");
//        List<BusinessTeacherFlowRecordVO> teacherApplyList = teacherService.getTeacherApplyList(id);
//        return ResultUtils.success(teacherApplyList);
//    }




    /**
     * 根据教师Id判断当前用户是否存在关联教师
     * @param teacherId
     * @return
     */
    private Boolean checkUserTeacher(String teacherId){
        if(!StpUtil.getRoleList().contains("ROLE_ADMIN")){
            //只允许修改本人
            String userId = (String) StpUtil.getLoginId();
            Teacher teacher = teacherService.getTeacherByUserId(userId);

            if(ObjectUtils.isEmpty(teacher)){
                return false;
            }
        }
        return true;
    }

}
